package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.feign.staffFeign;
import com.buba.mapper.AccumulationMapper;
import com.buba.mapper.AdjustmentRecordMapper;
import com.buba.mapper.InsuranceFileMapper;
import com.buba.mapper.SocialMapper;
import com.buba.service.InsuranceFileService;
import common.R;
import entity.salary.Accumulation;
import entity.salary.AdjustmentRecord;
import entity.salary.InsuranceFile;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.salary.Social;
import entity.salary.dto.AccountDto;
import entity.salary.vo.selAccSocialAccumVo;
import entity.salary.vo.selInsuranceFileVo;
import entity.salary.vo.socialDetailedVo;
import entity.staff.dto.saveAccountDto;
import entity.staff.vo.selAccVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-20
 */
@Service
public class InsuranceFileServiceImpl extends ServiceImpl<InsuranceFileMapper, InsuranceFile> implements InsuranceFileService {

    @Resource
    private InsuranceFileMapper insuranceFileMapper;
    @Resource
    private staffFeign staffFeign;
    @Resource
    private SocialMapper socialMapper;
    @Resource
    private AccumulationMapper accumulationMapper;
    @Resource
    private SocialServiceImpl socialService;
    @Resource
    private AccumulationServiceImpl accumulationService;
    @Resource
    private AdjustmentRecordMapper adjustmentRecordMapper;

    /* *
     * @Author: beisheng
     * @Descriprion: 进行增员调整,如果是调整方案，那么进行添加调整记录
     * @Date: 2023/3/20 19:32
     * @Return: common.R
     * @Param: [saveAccountDto]
     */

    @Override
    public void saveAccount(AccountDto saveAccountDto) {
        InsuranceFile insuranceFile = new InsuranceFile();
        BeanUtils.copyProperties(saveAccountDto,insuranceFile);
        if(saveAccountDto.getInsuranceId() == null){
            insuranceFileMapper.insert(insuranceFile);
            saveAccountDto.setSaId(insuranceFile.getSaId());
            staffFeign.updateAccSocialAndAccum(saveAccountDto);
        }else {
            InsuranceFile insuranceFileOne = insuranceFileMapper.selectOne(new QueryWrapper<InsuranceFile>().eq("sa_id", saveAccountDto.getInsuranceId()));
            Social social = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag", 0).eq("sa_id", insuranceFileOne.getSocialId()));
            Accumulation accumulation = accumulationMapper.selectOne(new QueryWrapper<Accumulation>().eq("delete_flag", 0).eq("sa_id", insuranceFileOne.getAccumId()));
            AdjustmentRecord adjustmentRecord = new AdjustmentRecord();
            adjustmentRecord.setSocialHitory(social.getSchemeName());
            adjustmentRecord.setAccumHistory(accumulation.getSchemeName());
            Social socialNew = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag", 0).eq("sa_id", saveAccountDto.getSocialId()));
            Accumulation accumulationNew = accumulationMapper.selectOne(new QueryWrapper<Accumulation>().eq("delete_flag", 0).eq("sa_id", saveAccountDto.getAccumId()));
            adjustmentRecord.setSocialNow(socialNew.getSchemeName());
            adjustmentRecord.setAccumNow(accumulationNew.getSchemeName());
            adjustmentRecord.setSocialHistorycardinal(saveAccountDto.getSocialCardinal());
            adjustmentRecord.setAccumHistorycardinal(saveAccountDto.getAccumCardinal());
            adjustmentRecord.setSocialNowcardinal(insuranceFileOne.getSocialCardinal());
            adjustmentRecord.setAccumNowcardinal(insuranceFileOne.getAccumCardinal());
            adjustmentRecord.setCreateTime(new Date());
            adjustmentRecord.setForceMonth(new Date());
            adjustmentRecordMapper.insert(adjustmentRecord);
            insuranceFileMapper.update(insuranceFile,new QueryWrapper<InsuranceFile>().eq("sa_id",saveAccountDto.getInsuranceId()));
        }

    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询个人参保档案
     * @Date: 2023/3/21 14:42
     * @Return: common.R
     * @Param: [insuranceId]
     */

    @Override
    public Object selInsuranceFile(Integer insuranceId) {
        selInsuranceFileVo selInsuranceFileVo = new selInsuranceFileVo();
        InsuranceFile insuranceFile = insuranceFileMapper.selectOne(new QueryWrapper<InsuranceFile>().eq("sa_id", insuranceId));
        Social social = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag", 0).eq("sa_id", insuranceFile.getSocialId()));
        Accumulation accumulation = accumulationMapper.selectOne(new QueryWrapper<Accumulation>().eq("delete_flag", 0).eq("sa_id", insuranceFile.getAccumId()));
        selInsuranceFileVo.setSocialName(social.getSchemeName());
        selInsuranceFileVo.setAccumName(accumulation.getSchemeName());
        selInsuranceFileVo.setAccumAccount(insuranceFile.getAccumAccounts());
        selInsuranceFileVo.setSocialAccount(insuranceFile.getSocialAccounts());
        selInsuranceFileVo.setAccumCardinal(insuranceFile.getAccumCardinal());
        selInsuranceFileVo.setSocialCardinal(insuranceFile.getSocialCardinal());
        selInsuranceFileVo.setAccumCardinal(insuranceFile.getAccumCardinal());
        selInsuranceFileVo.setSocialStart(insuranceFile.getSocialStart());
        selInsuranceFileVo.setAccumStart(insuranceFile.getAccumStart());
        selInsuranceFileVo.setSocialRemark(insuranceFile.getSocialRemark());
        selInsuranceFileVo.setAccumRemark(insuranceFile.getAccumRemark());
        List<socialDetailedVo> socialDetailedVos = socialService.socialDetailed(insuranceFile.getSocialId());
        List<socialDetailedVo> accumDetailedVos = accumulationService.accumDetailed(insuranceFile.getAccumId());
        selInsuranceFileVo.setAccumDetailedVos(accumDetailedVos);
        selInsuranceFileVo.setSocialDetailedVos(socialDetailedVos);
        return selInsuranceFileVo;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据档案ID查询社保公积金基本信息
     * @Date: 2023/3/21 16:25
     * @Return: common.R
     * @Param: [insurId]
     */

    @Override
    public selAccSocialAccumVo selSocialAndAccum(Integer insurId) {
        selAccSocialAccumVo selAccVo = new selAccSocialAccumVo();
        InsuranceFile insuranceFile = insuranceFileMapper.selectOne(new QueryWrapper<InsuranceFile>().eq("sa_id", insurId));
        Social social = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag", 0).eq("sa_id", insuranceFile.getSocialId()));
        Accumulation accumulation = accumulationMapper.selectOne(new QueryWrapper<Accumulation>().eq("delete_flag", 0).eq("sa_id", insuranceFile.getAccumId()));
        selAccVo.setSocialName(social.getSchemeName());
        selAccVo.setAccumName(accumulation.getSchemeName());
        selAccVo.setSocialCardinal(insuranceFile.getSocialCardinal());
        selAccVo.setAccumCardinal(insuranceFile.getAccumCardinal());
        return selAccVo;
    }
}
